package com.jrx.anytxn.param.controller.partner;

import com.google.common.collect.Maps;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.partner.PrVerifyFileTableReq;
import com.jrx.anytxn.param.dto.partner.PrVerifyFileTableRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.partner.IPrVerifyFileTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(tags = "合作方对账文件参数配置API")
@RestController
@RequestMapping(value = "/param/partner/verifyFile")
public class PrVerifyFileController {

    private static final Logger logger = LoggerFactory.getLogger(PrVerifyFileController.class);

    @Autowired
    private InfoService infoService;
    @Autowired
    private IPrVerifyFileTableService prVerifyFileTableService;

    @ApiOperation(value = "根据租户ID和对账配置参数ID查询", notes = "根据租户ID和商户ID查询")
    @GetMapping(value = "/{tenantId}/{verifyFileId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "verifyFileId", value = "对账文件配置参数ID", required = true)
    })
    public TxnRespResult<PrVerifyFileTableRes> getById(@PathVariable String tenantId,
                                                       @PathVariable String verifyFileId) {
        TxnRespResult<PrVerifyFileTableRes> result = new TxnRespResult<>();
        PrVerifyFileTableRes res;
        try {
            res = prVerifyFileTableService.getByTenIdIdAndVerifyFileId(tenantId, verifyFileId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "新增对账文件配置参数", notes = "新增对账文件配置参数")
    @PostMapping("/")
    public TxnRespResult<PrVerifyFileTableRes> add(@RequestBody PrVerifyFileTableReq prVerifyFileTableReq) {
        TxnRespResult<PrVerifyFileTableRes> result = new TxnRespResult<>();
        PrVerifyFileTableRes res;
        try{
            prVerifyFileTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prVerifyFileTableReq.setUpdateBy(infoService.getUser().getUserName());
            res = prVerifyFileTableService.addPrVerifyFileTable(prVerifyFileTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "分页查询对账文件配置参数", notes = "分页查询对账文件配置参数")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<PrVerifyFileTableRes>> getAllPageByTenId(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<PrVerifyFileTableRes>> result = new TxnRespResult<>();
        try {
            String tenantId=String.valueOf(infoService.getUser().getProjectId());
            TxnPage<PrVerifyFileTableRes>  res = prVerifyFileTableService.findAllPageByTenantId(pageNum, pageSize, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "更新对账文件配置参数", notes = "更新对账文件配置参数")
    @PutMapping("/")
    public TxnRespResult<PrVerifyFileTableRes> modifyPmPartnerInfo(@RequestBody PrVerifyFileTableReq req) {
        TxnRespResult<PrVerifyFileTableRes> result = new TxnRespResult<>();
        PrVerifyFileTableRes res;
        try {
            req.setUpdateBy(infoService.getUser().getUserName());
            res = prVerifyFileTableService.modifyPrVerifyFileTable(req);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和参数ID删除信息", notes = "根据租户ID和参数ID删除信息")
    @DeleteMapping(value = "/{tenantId}/{verifyFileId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "verifyFileId", value = "对账文件配置参数id", required = true)
    })
    public TxnRespResult<Boolean> removeByTenIdAndVerifyFileId(@PathVariable String tenantId, @PathVariable String verifyFileId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            Boolean flag =
                    prVerifyFileTableService.removeByTenantIdAndVerifyFileId(tenantId, verifyFileId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "获得对账文件配置下拉信息", notes = "获得对账文件配置下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PrVerifyFileTableRes>>> getVerifyFileList() {
        TxnRespResult<HashMap<String, List<PrVerifyFileTableRes>>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PrVerifyFileTableRes> all = prVerifyFileTableService.findAllByTenId(tenantId);
        HashMap<String, List<PrVerifyFileTableRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", all);
        return result.getSuccess(sysCodeMap);
    }

    @ApiOperation(value = "根据机构号和租户ID查询对账文件参数", notes = "根据机构号和租户ID查询对账文件参数")
    @GetMapping(value = "/organizationId/{organizationId}")
    @ApiImplicitParam(name = "organizationId", value = "机构号", required = true)
    public TxnRespResult<ArrayList<PrVerifyFileTableRes>> getVerifyFileByOrgId(@PathVariable String organizationId) {
        TxnRespResult<ArrayList<PrVerifyFileTableRes>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PrVerifyFileTableRes> list = prVerifyFileTableService.getVerifyFileByOrgIdAndTenId(organizationId, tenantId);
        return result.getSuccess((ArrayList) list);
    }
}
